// utils/routeUtils.ts
import type { RouteRecordRaw } from 'vue-router'
import Layout from "@/layout/index.vue"
// 自动导入 views 目录下的组件
const modules = import.meta.glob('/src/views/**/*.vue')
// 转换后端路由数据
export function transformRoutes(routes: any[]): RouteRecordRaw[] {

    routes = routes.filter(route => route.path !== null && route.path !== '')
    return routes.map(route => {

        const transformedRoute: RouteRecordRaw = {
            path: route.path,
            component: resolveComponent(route.component),
            meta: route.meta,
            children: []
        }
        return transformedRoute
    })

}

// 解析组件路径
function resolveComponent(componentPath: string) {
    // 匹配视图组件
    const moduleKey = Object.keys(modules).find(key =>
        key.includes(`/src/views/${componentPath}.vue`)
    )
    return moduleKey ? modules[moduleKey] : () => import('@/views/error/404.vue')
}